home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / EGAVGA.SWG / 0170_Fast Polygon Fill.pas < prev    next >
Pascal/Delphi Source File  |  1995-03-03  |  3KB  |  91 lines

  1. {
  2. Here is a fast polygon fill I wrote in an hour. It requires that the polygon
  3. is convex, and the verticies are in sequential order. Right now, the verticies
  4. must be in counter clockwise rotation, but that can be changed by removing
  5. the conditional statement in the HLine procedure. It handles an arbitrary
  6. number of verticies.
  7.  
  8. achalfin@uceng.uc.edu
  9. }
  10.  
  11. Procedure HLine(X1, X2, Y : Integer; Color : Byte);
  12. { Fill using words to speed up alot! }
  13.  
  14. Label Offscreen;
  15.  
  16. Begin
  17.   If Y < 0 Then Goto Offscreen;
  18.   If Y > 199 Then Goto Offscreen;
  19.   If X2 > X1
  20.     Then Begin
  21.       If X1 < 0 Then X1 := 0;
  22.       If X2 > 319 Then X2 := 319;
  23.       FillChar(Mem[$A000:Y*320+X1], X2-X1+1, Color);
  24.     End;
  25.  OffScreen:
  26. End;
  27.  
  28. Procedure FillPoly(P:MappedCoords; Num : Word; Color : Byte);
  29. { Mapped Coords is an array of (x, y) coords, each is an integer }
  30.  
  31. Var
  32.   x1, y1, x2, y2, x11, y11, x22, y22 : Integer;
  33.   StartV1, EndV1, StartV2, EndV2 : Integer;
  34.   Dx1, Dx2, Count1, Count2 : Integer;
  35.   XVal1, XVal2 : Longint;
  36.   EdgeCount, MinY : Word;
  37.   C : Integer;
  38.  
  39. Begin
  40.   EdgeCount := Num;
  41.   MinY := 300;
  42.   For C := 0 to (EdgeCount-1) do  { Find Top Vertex }
  43.     Begin
  44.       If P[c].Y < MinY
  45.         Then Begin MinY := P[c].Y; StartV1 := C; End;
  46.     End;
  47.   StartV2 := StartV1;
  48.   EndV1 := StartV1 - 1;
  49.   EndV2 := StartV2 + 1;
  50.   If EndV1 < 0 Then EndV1 := (Num-1);
  51.   If EndV2 >= Num Then EndV2 := 0;
  52.   MinY := P[StartV1].Y;
  53.   X1:=P[StartV1].X; Y1:=P[StartV1].Y; X2:=P[EndV1].X; Y2:=P[EndV1].Y;
  54.   Dx1 := ((X2 - X1) Shl 8) Div (Y2 - Y1 + 1);
  55.   Count1 := Y2-Y1; XVal1 := Longint(X1) Shl 8;
  56.   X11:=P[StartV2].X; Y11:=P[StartV2].Y; X22:=P[EndV2].X; Y22:=P[EndV2].Y;
  57.   Dx2 := ((X22 - X11) Shl 8) Div (Y22 - Y11 + 1);
  58.   Count2 := Y22-Y11;  XVal2 := Longint(X11) Shl 8;
  59.   While EdgeCount > 1 do
  60.     Begin
  61.       While (Count1 > 0) and (Count2 > 0) do
  62.         Begin
  63.           HLine(XVal1 Shr 8, XVal2 Shr 8, MinY, Color);
  64.           XVal1 := XVal1 + Dx1; XVal2 := XVal2 + Dx2;
  65.           Count1 := Count1 - 1; Count2 := Count2 - 1;
  66.           MinY := MinY + 1;
  67.         End;
  68.       If Count1 = 0
  69.         Then Begin
  70.           StartV1 := EndV1;  EndV1 := EndV1 - 1;
  71.           If EndV1 < 0 Then EndV1 := (Num-1);
  72.           EdgeCount := EdgeCount - 1; MinY := P[StartV1].Y;
  73.           X1 := P[StartV1].X; Y1 := P[StartV1].Y;
  74.           X2 := P[EndV1].X; Y2 := P[EndV1].Y;
  75.           Dx1 := ((X2 - X1) Shl 8) Div (Abs(Y2 - Y1) + 1);
  76.           Count1 := Y2-Y1; XVal1 := Longint(X1) Shl 8;
  77.         End;
  78.       If Count2 = 0
  79.         Then Begin
  80.           StartV2 := EndV2; EndV2 := EndV2 + 1;
  81.           If EndV2 >= Num  Then EndV2 := 0;
  82.           EdgeCount := EdgeCount - 1; MinY := P[StartV2].Y;
  83.           X11 := P[StartV2].X; Y11 := P[StartV2].Y;
  84.           X22 := P[EndV2].X; Y22 := P[EndV2].Y;
  85.           Dx2 := ((X22 - X11) Shl 8) Div (Abs(Y22 - Y11) + 1);
  86.           Count2 := Y22-Y11; XVal2 := Longint(X11) Shl 8;
  87.         End;
  88.     End;
  89. End;
  90.  
  91.